package com.accp.restaurant.service;

import com.accp.restaurant.entity.Orders;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class OrderService {

    @PersistenceContext
    private EntityManager entityManager;

    public int add(String orderId,String ordertime,long orderState,long dishesid,long roomid,long dishesnum,double totalamount){
        String jpql = "insert into orders(orderId,ordertime,orderState,dishesid,roomid,dishesnum,totalamount)values(?,?,?,?,?,?,?)";
        Query query = entityManager.createNativeQuery(jpql).setParameter(1,orderId).setParameter(2,ordertime).setParameter(3,orderState).setParameter(4,dishesid).setParameter(5,roomid)
                .setParameter(6,dishesnum).setParameter(7,totalamount);
        return query.executeUpdate();
    }


    /**
     * 多条件查询
     * @param pageNumber 当前页
     * @param pageSize 每也显示多少条数据
     * @param orderid 订单编号
     * @param ordertime 订单时间
     * @return list集合
     */
    public List<Orders> query(Integer pageNumber, Integer pageSize, String orderid,String ordertime){
        String jpql = "select o,sum(o.totalamount) from Orders o join fetch o.dishes where 1=1";
        if(orderid!=null&&!orderid.equals("")){
            jpql +=" and o.orderid like :orderid";
        }
        if(ordertime!=null&&!ordertime.equals("")){
            jpql +=" and o.ordertime like :ordertime group by o.orderid";
        }else{
            jpql +=" group by o.orderid";
        }
        Query query = entityManager.createQuery(jpql);
        if(orderid!=null&&!orderid.equals("")){
            query.setParameter("orderid",orderid);
        }
        if(ordertime!=null&&!ordertime.equals("")){
            query.setParameter("ordertime",ordertime);
        }
        query.setFirstResult((pageNumber-1)*pageSize);
        query.setMaxResults(pageSize);
        return query.getResultList();
    }

    /**
     * 多条件查询总条数
     * @param pageNumber 当前页
     * @param pageSize 每页显示多少条数据
     * @param orderid 订单编号
     * @param ordertime 订单时间
     * @return 总条数
     */
    public long getCount(Integer pageNumber, Integer pageSize, String orderid,String ordertime){
        String jpql = "select count(distinct o.orderid) from Orders o inner join o.dishes od  where 1=1";
        if(orderid!=null&&!orderid.equals("")){
            jpql +=" and o.orderid like :orderid";
        }
        if(ordertime!=null&&!ordertime.equals("")){
            jpql +=" and o.ordertime like :ordertime ";
        }
        Query query = entityManager.createQuery(jpql);
        if(orderid!=null&&!orderid.equals("")){
            query.setParameter("orderid",orderid);
        }
        if(ordertime!=null&&!ordertime.equals("")){
            query.setParameter("ordertime",ordertime);
        }
        return (long)query.getSingleResult();
    }








    /**
     * 多条件查询
     * @param pageNumber 当前页
     * @param pageSize 每也显示多少条数据
     * @param orderid 订单编号
     * @return list集合
     */
    public List<Orders> findById(Integer pageNumber, Integer pageSize, String orderid){
        String jpql = "select o from Orders o join fetch o.dishes where 1=1";
        if(orderid!=null&&!orderid.equals("")){
            jpql +=" and o.orderid = :orderid";
        }
        Query query = entityManager.createQuery(jpql);
        if(orderid!=null&&!orderid.equals("")){
            query.setParameter("orderid",orderid);
        }
        return query.getResultList();
    }
}
